/*******************************************************************************
 * Copyright 2011 Andrea Guitto
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.andreaguitto.mobile.android.framework.base.object.activity;

import java.util.ArrayList;
import java.util.Hashtable;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Shader.TileMode;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

import com.andreaguitto.mobile.android.framework.base.interfaces.AGActivityLayerInterface;
import com.andreaguitto.mobile.android.framework.base.interfaces.activities.AGActivityLayerCompatibleInterface;
import com.andreaguitto.mobile.android.framework.base.interfaces.activities.AGBaseActivityInterface;
import com.andreaguitto.mobile.android.framework.base.workers.AGActivityLayerCompatibleManager;
import com.andreaguitto.mobile.android.framework.base.workers.AGBaseActivityManager;

public abstract class AGActivity extends Activity implements AGBaseActivityInterface, AGActivityLayerCompatibleInterface {

	static final int NETWORK_STATUS = 1;
	
	private ProgressDialog currentProgressDialog;
	private Hashtable<String, Object> activityLayersSharedObjects;
	private ArrayList<AGActivityLayerInterface> activityLayers; 
	
	@Override
	protected void onSaveInstanceState(Bundle state) {
		Log.d(getLogTag(), "onSaveInstanceState()");
		AGActivityLayerCompatibleManager.manageOnSaveInstanceState(this, activityLayers, state);
		super.onSaveInstanceState(state);
	}

	@Override
	protected void onRestoreInstanceState(Bundle state) {
		super.onRestoreInstanceState(state);
		AGActivityLayerCompatibleManager.manageOnRestoreInstanceState(this, activityLayers, state);
		
		Log.d(getLogTag(), "onRestoreInstanceState()");
	}
	
	@Override
	/**
	 * call dismissDialog(id) encapsulated in a try - catch
	 */	
	public void dismissDialogOrTry(int id){
		try{
			dismissDialog(id);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@Override
	/**
	 * call removeDialog(id) encapsulated in a try - catch
	 */
	public void removeDialogOrTry(int id){
		try{
			removeDialog(id);
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);		
		Log.d(getLogTag(), "onCreate()");
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	    		
		AGActivityLayerCompatibleManager.manageOnCreate(this, activityLayers, savedInstanceState);
	}
		
	@Override
	protected void onStart(){
		super.onStart();		
		Log.d(getLogTag(), "onStart()");
		
		AGActivityLayerCompatibleManager.manageOnStart(this, activityLayers);
	}

	@Override
	protected void onResume(){
		super.onResume();		
		Log.d(getLogTag(), "onResume()");
		
		AGActivityLayerCompatibleManager.manageOnResume(this, activityLayers);
	}

	@Override
	protected void onPause(){
		super.onPause();
		Log.d(getLogTag(), "onPause()");

		AGActivityLayerCompatibleManager.manageOnPause(this, activityLayers);		
		
	}
	
	@Override
	protected void onStop(){
		super.onStop();
		Log.d(getLogTag(), "onStop()");
		
		AGActivityLayerCompatibleManager.manageOnStop(this, activityLayers);
	}

	@Override
	protected void onDestroy(){
		super.onDestroy();
		Log.d(getLogTag(), "onDestroy()");
		
		AGActivityLayerCompatibleManager.manageOnDestroy(this, activityLayers);
		
		if(currentProgressDialog!=null){
			currentProgressDialog.dismiss();
		}
		
		if(activityLayers!=null){
			activityLayers.clear();
			activityLayers = null;
		}
	}
	
	
	public ProgressDialog showProgressDialog() {
		if(currentProgressDialog==null){
			currentProgressDialog = ProgressDialog.show(this, "Loading", "Loading...",true, true, null);
		}else if(!currentProgressDialog.isShowing()){
			currentProgressDialog.show();
		}
		return currentProgressDialog;
	}
	
	public ProgressDialog showProgressDialog(String title, String message, boolean indeterminate, boolean canceleable, android.content.DialogInterface.OnCancelListener listener) {
		if(currentProgressDialog==null){
			currentProgressDialog = ProgressDialog.show(this, title, message, indeterminate, canceleable, listener);
		}else if(!currentProgressDialog.isShowing()){
			currentProgressDialog.dismiss();
			currentProgressDialog = ProgressDialog.show(this, title, message, indeterminate, canceleable, listener);
		}
		return currentProgressDialog;
	}

	public ProgressDialog dismissProgressDialog(){
		if(currentProgressDialog!=null)
			currentProgressDialog.dismiss();
		return currentProgressDialog;
	}
	
	public ProgressDialog cancelProgressDialog(){
		if(currentProgressDialog!=null)
			currentProgressDialog.cancel();
		return currentProgressDialog;
	}	
	
	public ProgressDialog getProgressDialog(){
		return currentProgressDialog;
	}	
	
	public void setBackgroundRepeatedDrawable(int backgroundViewResource, int drawableTileResource, TileMode xTileMode, TileMode yTileMode){	
	    AGBaseActivityManager.manageSetBackgroundRepeatedDrawable(this, backgroundViewResource, drawableTileResource, xTileMode, yTileMode);
	}	
	
	public void showToast(Context mContext, String text) {
		AGBaseActivityManager.manageShowToast(this, mContext, text);
	}
	
	public void startActivityByAction(int resourceAction){
		AGBaseActivityManager.manageStartActivityByAction(this, resourceAction);		
	}

	public void startActivityByAction(int resourceAction, Bundle extras){
		AGBaseActivityManager.manageStartActivityByAction(this, resourceAction, extras);		
	}	
	
	public void addActivityLayer(AGActivityLayerInterface filter){
		activityLayers=(ArrayList<AGActivityLayerInterface>) AGActivityLayerCompatibleManager.manageAddActivityLayer(this, activityLayers, filter);
	}
	
	public void removeActivityLayer(AGActivityLayerInterface filter){
		activityLayers=(ArrayList<AGActivityLayerInterface>) AGActivityLayerCompatibleManager.manageRemoveActivityLayer(this, activityLayers, filter);
	}
	
	public void clearActivityLayers(){
		activityLayers=(ArrayList<AGActivityLayerInterface>) AGActivityLayerCompatibleManager.manageClearActivityLayers(this, activityLayers);
	}
	
	public void onBeforeLayerAction(AGActivityLayerInterface filter, int action){
		Log.d(getLogTag(), filter.getClass().getName()+".onBeforeActivityLayerAction("+action+")");
	}
	
	public void onAfterLayerAction(AGActivityLayerInterface filter, int action){
		Log.d(getLogTag(), filter.getClass().getName()+".onAfterActivityLayerAction("+action+")");
	}

	public String getLogTag(){
		return this.getClass().getName();
	}
	
	@Override
	public Hashtable<String, Object> getActivityLayersSharedObjects() {
		if(activityLayersSharedObjects==null){
			activityLayersSharedObjects = AGActivityLayerCompatibleManager.initSharedObjects(this);
		}
		return activityLayersSharedObjects;	
	}		
}
